package com.app.web.front.model;

import com.app.util.MyUtil;
import com.app.web.BaseModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * msg_type  (1:动态, 2:留言, 3:系统留言)
 * @author Administrator
 *
 */
public class Message extends BaseModel<Message>{
	
	private static final long serialVersionUID = 7925448656579052520L;
	public static final Message dao = new Message();
	
	public Page<Record> paginateByUserId(int pageNumber, int pageSize,Record params){
		String userId = params.get("user_id", "0");
		int type = MyUtil.parseInt(params.get("type", "0"),0);
		int msgType = MyUtil.parseInt(params.get("msg_type", "1"),1);//1、说说，2、留言
		
		String select="select a.*, b.user_name as sender_name, c.user_name as receiver_name,b.pic_url as sender_pic, c.pic_url as receiver_pic,"+
							"IFNULL(browse_num,0) as browse_num,"+ 
							"(SELECT COUNT(id) FROM vote WHERE type_id=a.id AND TYPE=2) as vote_num,"+
							"(SELECT COUNT(id) FROM COMMENT WHERE type_id=a.id AND TYPE=2) AS comment_num ";	
		
		String from="";//from语句
		Object[] paras=null;//参数
		
		if(type==1){//我的说说
			paras=new Object[]{userId,userId,msgType};
			from="from message a "+
				"left outer join user b on a.sender_id = b.id "+
			    "left outer join user c on a.receiver_id = c.id "+
			    "where (sender_id=? or receiver_id = ?) and msg_type = ? "+
			    "order by a.id desc";
		}else if(type==2){//与我相关
			paras=new Object[]{userId,userId};
			select="SELECT a.id,a.type_id,b.id,b.sender_id,b.content,b.msg_type,b.create_time,COUNT(a.id) AS num,c.user_name AS sender_name,c.pic_url as receiver_pic ";
			from="from COMMENT a INNER JOIN message b ON a.type_id=b.id INNER JOIN USER c ON b.sender_id=c.id  "
					+ "WHERE (a.sender_id=? OR a.receiver_id=?)  GROUP BY a.type_id order by a.id desc";	
			
		}else if(type==3){
			int type_id=MyUtil.parseInt(params.get("type_id", "0"),0);
			paras=new Object[]{type_id,userId,userId};
			select = "SELECT "+
						"a.id,a.type_id,a.type,a.sender_id,a.receiver_id,a.content,a.create_time,"+
						"b.id AS send_id,b.user_name AS send_name,b.pic_url AS sender_pic,"+
						"c.id AS receiv_id,c.user_name AS receiv_name,c.pic_url AS receiv_pic ";
			from = " FROM COMMENT a "+
						" INNER JOIN USER b ON  a.sender_id = b.id "+
						" INNER JOIN USER c ON  a.receiver_id = c.id "+
						" WHERE a.type_id = ? AND (a.sender_id = ? OR a.receiver_id = ?)";
			
		}else if(type==20){//留言板
			paras=new Object[]{userId,msgType};
			from="from message a "+
					"left outer join user b on a.sender_id = b.id "+
				    "left outer join user c on a.receiver_id = c.id "+
				    "where a.receiver_id=? and msg_type = ? "+
				    "order by a.id desc";
		}else{//全部、好友动态
			paras=new Object[]{msgType};
			from="from message a "+
					"left outer join user b on a.sender_id = b.id "+
				    "left outer join user c on a.receiver_id = c.id "+
				    "where msg_type = ? "+
				    "order by a.id desc";
		}
		return Db.paginate(pageNumber, pageSize, select, from, paras);
		//return Message.dao.paginate(pageNumber, pageSize, select, from, obj);
	}
	
	public int vote(Record params){
		int msg_id=MyUtil.parseInt(params.get("msg_id", "0"),0);
		int user_id=MyUtil.parseInt(params.get("user_id", "0"),0);
		Integer id=Db.queryInt("select id from vote where type_id="+msg_id+" and type=2 and vote=1 and user_id="+user_id);
		
		Db.update("update message set browse_num=browse_num+1 where id="+msg_id);//增加浏览量
		if(id==null){//加赞
			Db.update("insert into vote(user_id,type_id,type,vote,create_time) values("+user_id+","+msg_id+",2,1,now())");
			return 1;
		}/*else{
			Db.update("delete from vote where id="+id);
			return -1;
		}*/
		return 0;
	}
}
